<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CPP11ThreadPool: runnable.hpp 源文件</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="CPP.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">CPP11ThreadPool
   </div>
   <div id="projectbrief">C++11线程池</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'搜索');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','搜索');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">runnable.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#ifndef RUNNABLE_HPP</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#define RUNNABLE_HPP</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="preprocessor">#include &lt;functional&gt;</span></div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="preprocessor">#include &lt;memory&gt;</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="preprocessor">#include &quot;functor_wrapper.hpp&quot;</span></div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment">//                            执行operator()可以运行任务</span></div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment">//                            千万不能把两个Runnable对象循环赋值</span></div><div class="line"><a name="l00013"></a><span class="lineno"><a class="line" href="classRunnable.html">   13</a></span>&#160;<span class="keyword">class </span><a class="code" href="classRunnable.html">Runnable</a> {</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;    <span class="keyword">public</span>:</div><div class="line"><a name="l00018"></a><span class="lineno"><a class="line" href="classRunnable.html#abe8d3066c7305401d6f0aad8e70780f2">   18</a></span>&#160;        <span class="keyword">using</span> <a class="code" href="classRunnable.html#abe8d3066c7305401d6f0aad8e70780f2">sptr</a> = std::shared_ptr&lt;Runnable&gt;;</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> F&gt;</div><div class="line"><a name="l00026"></a><span class="lineno"><a class="line" href="classRunnable.html#ab052afa8b53dd1e7c28e978962839446">   26</a></span>&#160;        <a class="code" href="classRunnable.html#ab052afa8b53dd1e7c28e978962839446">Runnable</a>(F&amp;&amp; f): <a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a>(new <a class="code" href="structFunctor__t.html">Functor_t</a>&lt;F&gt;(std::move(f))) {}</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;</div><div class="line"><a name="l00033"></a><span class="lineno"><a class="line" href="classRunnable.html#a6d6ce997a7873530a2f339412e6c4935">   33</a></span>&#160;        <span class="keyword">explicit</span> <a class="code" href="classRunnable.html#a6d6ce997a7873530a2f339412e6c4935">Runnable</a>(<a class="code" href="classRunnable.html">Runnable</a> &amp;&amp; rh): <a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a>(std::move(rh.<a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a>)) {}</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;</div><div class="line"><a name="l00040"></a><span class="lineno"><a class="line" href="classRunnable.html#a9e4d88b5d30d6e0368c5d22a0df203ee">   40</a></span>&#160;        <span class="keyword">explicit</span> <a class="code" href="classRunnable.html#a9e4d88b5d30d6e0368c5d22a0df203ee">Runnable</a>(<a class="code" href="classRunnable.html">Runnable</a> &amp; rh): <a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a>(std::move(rh.<a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a>)) {}</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;</div><div class="line"><a name="l00049"></a><span class="lineno"><a class="line" href="classRunnable.html#a572b61c98700ada8d241b122ddc80e2c">   49</a></span>&#160;        <a class="code" href="classRunnable.html">Runnable</a>&amp; <a class="code" href="classRunnable.html#a572b61c98700ada8d241b122ddc80e2c">operator=</a>(<a class="code" href="classRunnable.html">Runnable</a> &amp;&amp; rh) {</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;            <a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a> = std::move(rh.functor_);</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;            <span class="keywordflow">return</span> *<span class="keyword">this</span>;</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;        }</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;</div><div class="line"><a name="l00061"></a><span class="lineno"><a class="line" href="classRunnable.html#a4dca857ef9f64534673170bc3c8b72a1">   61</a></span>&#160;        <a class="code" href="classRunnable.html">Runnable</a>&amp; <a class="code" href="classRunnable.html#a4dca857ef9f64534673170bc3c8b72a1">operator=</a>(<a class="code" href="classRunnable.html">Runnable</a> &amp; rh) {</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;            <a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a> = std::move(rh.<a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a>);</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;            <span class="keywordflow">return</span> *<span class="keyword">this</span>;</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;        }</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;        <a class="code" href="classRunnable.html#a5769222dcc4b2adca7723bfbdfa00cbb">Runnable</a>() = <span class="keywordflow">default</span>;</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;        <span class="keyword">virtual</span> <a class="code" href="classRunnable.html#a08557328273d2dfd3425507b1f972fcb">~Runnable</a>() = <span class="keywordflow">default</span>;</div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;</div><div class="line"><a name="l00078"></a><span class="lineno"><a class="line" href="classRunnable.html#a38bf849dab4bbb86fc5bc6e7aff383e0">   78</a></span>&#160;        <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classRunnable.html#a38bf849dab4bbb86fc5bc6e7aff383e0">operator()</a>() {</div><div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;            <span class="keywordflow">if</span> (<a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a> != <span class="keyword">nullptr</span>) {</div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;                <a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a>-&gt;call();</div><div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;            }</div><div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;            <a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a>.release();</div><div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;        }</div><div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;</div><div class="line"><a name="l00090"></a><span class="lineno"><a class="line" href="classRunnable.html#a3abba14a5cf19709cef2d299ee68acae">   90</a></span>&#160;        <span class="keywordtype">bool</span> <a class="code" href="classRunnable.html#a3abba14a5cf19709cef2d299ee68acae">empty</a>()<span class="keyword"> const </span>{</div><div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;            <span class="keywordflow">return</span> <a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a> == <span class="keyword">nullptr</span>;</div><div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;        }</div><div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;</div><div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;    <span class="keyword">protected</span>:</div><div class="line"><a name="l00098"></a><span class="lineno"><a class="line" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">   98</a></span>&#160;        std::unique_ptr&lt;Functor_base&gt; <a class="code" href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">functor_</a>;</div><div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;};</div><div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;</div><div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;<span class="preprocessor">#endif </span><span class="comment">/* RUNNABLE_HPP */</span><span class="preprocessor"></span></div><div class="ttc" id="classRunnable_html_a08557328273d2dfd3425507b1f972fcb"><div class="ttname"><a href="classRunnable.html#a08557328273d2dfd3425507b1f972fcb">Runnable::~Runnable</a></div><div class="ttdeci">virtual ~Runnable()=default</div><div class="ttdoc">~Runnable 析构函数 </div></div>
<div class="ttc" id="structFunctor__t_html"><div class="ttname"><a href="structFunctor__t.html">Functor_t</a></div><div class="ttdef"><b>Definition:</b> functor_wrapper.hpp:16</div></div>
<div class="ttc" id="classRunnable_html_ab052afa8b53dd1e7c28e978962839446"><div class="ttname"><a href="classRunnable.html#ab052afa8b53dd1e7c28e978962839446">Runnable::Runnable</a></div><div class="ttdeci">Runnable(F &amp;&amp;f)</div><div class="ttdoc">Runnable 构造函数 </div><div class="ttdef"><b>Definition:</b> runnable.hpp:26</div></div>
<div class="ttc" id="classRunnable_html_a6d6ce997a7873530a2f339412e6c4935"><div class="ttname"><a href="classRunnable.html#a6d6ce997a7873530a2f339412e6c4935">Runnable::Runnable</a></div><div class="ttdeci">Runnable(Runnable &amp;&amp;rh)</div><div class="ttdoc">Runnable 复制构造 </div><div class="ttdef"><b>Definition:</b> runnable.hpp:33</div></div>
<div class="ttc" id="classRunnable_html_a38bf849dab4bbb86fc5bc6e7aff383e0"><div class="ttname"><a href="classRunnable.html#a38bf849dab4bbb86fc5bc6e7aff383e0">Runnable::operator()</a></div><div class="ttdeci">virtual void operator()()</div><div class="ttdoc">operator() 重载实现操作 </div><div class="ttdef"><b>Definition:</b> runnable.hpp:78</div></div>
<div class="ttc" id="classRunnable_html_a9e4d88b5d30d6e0368c5d22a0df203ee"><div class="ttname"><a href="classRunnable.html#a9e4d88b5d30d6e0368c5d22a0df203ee">Runnable::Runnable</a></div><div class="ttdeci">Runnable(Runnable &amp;rh)</div><div class="ttdoc">Runnable 拷贝构造不会复制functor_ </div><div class="ttdef"><b>Definition:</b> runnable.hpp:40</div></div>
<div class="ttc" id="classRunnable_html_a687268c89ddd0d1170687ba764cbc0f2"><div class="ttname"><a href="classRunnable.html#a687268c89ddd0d1170687ba764cbc0f2">Runnable::functor_</a></div><div class="ttdeci">std::unique_ptr&lt; Functor_base &gt; functor_</div><div class="ttdoc">函数包装器 </div><div class="ttdef"><b>Definition:</b> runnable.hpp:98</div></div>
<div class="ttc" id="classRunnable_html_abe8d3066c7305401d6f0aad8e70780f2"><div class="ttname"><a href="classRunnable.html#abe8d3066c7305401d6f0aad8e70780f2">Runnable::sptr</a></div><div class="ttdeci">std::shared_ptr&lt; Runnable &gt; sptr</div><div class="ttdoc">std::shared_ptr&lt;Runnable&gt;别名 </div><div class="ttdef"><b>Definition:</b> runnable.hpp:18</div></div>
<div class="ttc" id="classRunnable_html_a3abba14a5cf19709cef2d299ee68acae"><div class="ttname"><a href="classRunnable.html#a3abba14a5cf19709cef2d299ee68acae">Runnable::empty</a></div><div class="ttdeci">bool empty() const</div><div class="ttdoc">empty 判断内部的函数包装器是否为空 </div><div class="ttdef"><b>Definition:</b> runnable.hpp:90</div></div>
<div class="ttc" id="classRunnable_html_a4dca857ef9f64534673170bc3c8b72a1"><div class="ttname"><a href="classRunnable.html#a4dca857ef9f64534673170bc3c8b72a1">Runnable::operator=</a></div><div class="ttdeci">Runnable &amp; operator=(Runnable &amp;rh)</div><div class="ttdoc">operator= 复制 </div><div class="ttdef"><b>Definition:</b> runnable.hpp:61</div></div>
<div class="ttc" id="classRunnable_html"><div class="ttname"><a href="classRunnable.html">Runnable</a></div><div class="ttdoc">Runnable interface 重写operator()或传进lambda </div><div class="ttdef"><b>Definition:</b> runnable.hpp:13</div></div>
<div class="ttc" id="classRunnable_html_a5769222dcc4b2adca7723bfbdfa00cbb"><div class="ttname"><a href="classRunnable.html#a5769222dcc4b2adca7723bfbdfa00cbb">Runnable::Runnable</a></div><div class="ttdeci">Runnable()=default</div><div class="ttdoc">Runnable 默认构造 </div></div>
<div class="ttc" id="classRunnable_html_a572b61c98700ada8d241b122ddc80e2c"><div class="ttname"><a href="classRunnable.html#a572b61c98700ada8d241b122ddc80e2c">Runnable::operator=</a></div><div class="ttdeci">Runnable &amp; operator=(Runnable &amp;&amp;rh)</div><div class="ttdoc">operator= 复制 </div><div class="ttdef"><b>Definition:</b> runnable.hpp:49</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
制作者 &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
